package pro.lingwu.station.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import pro.lingwu.station.dao.BusMapper;
import pro.lingwu.station.dao.BusNoMapper;
import pro.lingwu.station.dao.TicketMapper;
import pro.lingwu.station.dto.PagesResult;
import pro.lingwu.station.dto.db.Bus;
import pro.lingwu.station.dto.db.BusNo;
import pro.lingwu.station.dto.db.Ticket;
import pro.lingwu.station.service.BusNoService;

import java.util.UUID;

/**
 * @author @lingwu
 * @date created in 12/19/2021
 */
@Service
public class BusNoServiceImpl implements BusNoService {

    private final BusNoMapper mapper;
    private final TicketMapper ticketMapper;
    private final BusMapper busMapper;

    public BusNoServiceImpl(BusNoMapper mapper, TicketMapper ticketMapper, BusMapper busMapper) {
        this.mapper = mapper;
        this.ticketMapper = ticketMapper;
        this.busMapper = busMapper;
    }

    @Override
    public PagesResult<BusNo> getAllBusNo(int start, int size, BusNo busNo, String startS, String endS) {
        if (size>0){
            PageHelper.startPage(start, size);
        }
        var res = new PagesResult<BusNo>();
        res.setResult(mapper.findBusNo(busNo, startS, endS));
        res.setTotal(mapper.getTotal());
        return res;
    }

    @Override
    public void addBusNo(BusNo busNo) {
        String id = UUID.randomUUID().toString().replace("-","");
        busNo.setId(id);
        mapper.addBusNo(busNo);
        Bus bus = busMapper.findBusBy(busNo.getBusNum());
        for (int i = 0; i < bus.getPersons(); i++) {
            Ticket ticket = new Ticket();
            ticket.setSeat(i+1);
            ticket.setNoId(id);
            ticketMapper.addTicket(ticket);
        }
    }

    @Override
    public void rmBusNo(String id) {
        mapper.rmBusNo(id);
    }

    @Override
    public void updateBusNo(BusNo busNo) {
        mapper.updateBusNo(busNo);
    }
}
